public class demo {
    public static MySingleList.ListNodes findpublic(MySingleList.ListNodes headA, MySingleList.ListNodes headB){
        MySingleList.ListNodes pL = headA;
        MySingleList.ListNodes pS = headB;
        int LenA = 0;
        int LenB = 0;
        while (pL != null) {
            LenA++;
            pL = pL.next;
        }
        while (pS != null){
            LenB++;
            pS = pS.next;
        }
        pL = headA;
        pS = headB;
        int Len = LenA - LenB;
        if (Len < 0){
            pL = headB;
            pS = headA;
            Len = LenB - LenA;
        }
        while (Len != 0){
            pL = pL.next;
            Len--;
        }
        while (pS != pL){
            pL = pL.next;
            pS =pS.next;
        }
        return pL;
    }
    private static MySingleList.ListNodes creatintersect(MySingleList.ListNodes headA, MySingleList.ListNodes headB){
        headB.next.next = headA.next.next;
        return headA;
    }


    public static MySingleList.ListNodes merge(MySingleList.ListNodes headA,MySingleList.ListNodes headB){
        MySingleList.ListNodes newHead = new MySingleList.ListNodes(-1);
        MySingleList.ListNodes tmpHead = newHead;
        while (headA != null && headB != null){
            if (headA.val > headB.val){
                tmpHead.next = headB;
                headB = headB.next;
            }else {
                tmpHead.next = headA;
                headA = headA.next;
            }
            tmpHead = tmpHead.next;
        }
        if (headA != null){
            tmpHead.next = headA;
        }
        if (headB != null){
            tmpHead.next = headB;
        }
        return newHead.next;
    }
    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(23);
        mySingleList1.addLast(34);
        mySingleList1.addLast(45);
        mySingleList1.addLast(56);
        mySingleList1.display();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(8);
        mySingleList2.addLast(15);
        mySingleList2.addLast(30);
        mySingleList2.addLast(40);
        mySingleList2.addLast(89);
        mySingleList2.display();

        MySingleList.ListNodes nh = merge(mySingleList1.head,mySingleList2.head);
        mySingleList1.display(nh);

    }
    public static void main1(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.creatList();
        mySingleList.display();
        mySingleList.resever();
        mySingleList.display();
        System.out.println();
        MySingleList.ListNodes listNodes = mySingleList.midList();
        System.out.println(listNodes.val);
        MySingleList.ListNodes ret = mySingleList.returnIndex(3);
        System.out.println(ret.val);
    }
}
